<?php
namespace Admin\Controller;

use Admin\Controller\BaseController;
use Home\Event\LetterEvent;
use Think\Controller;

class LetterController extends BaseController
{
    public $obj;
    public $temp_obj;
    public $local_id;

    public function __construct()
    {
        parent::__construct();
        $this->obj = M('store_letter');
        $this->_arr['title'] = '短信设置';
        $this->local_id = session('userInfo')['local_id'];
    }

    public function index()
    {
        $this->_arr['title_two'] = '短信列表';
        $this->_arr['p'] = I('p', 1, 'intval');
        $where = 'sl.local_id = '.$this->local_id;
        if($this->_arr['letter_type']){
            $where .= " and sl.slt_id = ".$this->_arr['letter_type'];
        }
        $sl_id = I('id') ? I('id') : 2;
        if ($sl_id == 1) {
            $order = 'sl_id asc';
            $this->_arr['rank'] = 2;
        } elseif ($sl_id == 2) {
            $order = 'sl_id desc';
            $this->_arr['rank'] = 1;
        }
        $data = $this->obj->alias('sl')->field('sl.*,slt.type_name')
            ->join("sdb_store_letter_type slt ON slt.slt_id = sl.slt_id ", 'left')
            ->where($where)->order($order)->page($this->_arr['p'] . ','.$this->page_num)->select();
        //echo M()->_sql();
        $type_data = M('store_letter_type')->getField('slt_id,type_name');
        $count = $this->obj->where($where)->count();// 查询满足要求的总记录数
        //dump($count);
        $Page = new \Think\Page($count, $this->page_num);// 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();// 分页显示输出
        //dump($show);
        $this->_arr['page'] = $show;// 赋值分页输出
        $this->_arr['list'] = $data;
        $this->_arr['type_data'] = $type_data;
        $this->_showDisplay();

    }

    /**
     * 新增
     */
    public function newLetter()
    {
        $this->_arr['title_two'] = '短信模板';
        $type_data = M('store_letter_type')->getField('slt_id,type_name');
        $this->_arr['type_data'] = $type_data;
        //类型
        $leeter_data = M('store_letter_type')->select();
        $type_data = [];
        $parameter_data = [];
        foreach($leeter_data as $k1 => $v1){
            $type_data[$v1['slt_id']] = $v1['type_name'];
            $parameter_data[$v1['slt_id']] = explode('#',$v1['parameter']);
        }
        $this->_arr['parameter_data'] = json_encode($parameter_data);
        $this->_showDisplay();
    }

    public function addOrSaveLetter(){
        $id = I('id',0);
        $data['slt_id'] = I('slt_id');
        $data['content'] = I('content');
        $data['local_id'] = $this->local_id;
        if($id){
            if(!is_numeric($id)){
                $this->error('参数错误');
            }
            $list = $this->obj->where(['sl_id' => $id])->find();
            /*dump($list['local_id']);
            dump($this->local_id);die;*/
            if($list['local_id'] != $this->local_id){
                $this->error('非法操作');
            }
            $status = $this->obj->where(['sl_id' => $id])->save($data);
            if($status === false){
                $this->error('修改失败');
            }
        }else{
            $data['create_time'] = time();
            $status = $this->obj->add($data);
            if($status === false){
                $this->error('新增失败');
            }
        }
        $this->success('成功', 'index');
    }

    /**
     * 修改
     */
    public function saveLetter()
    {
        $this->_arr['title_two'] = '短信模板';
        $id = I('id') ? I('id') : $this->error('参数为空！');
        if (!is_numeric($id)) $this->error('无效参数！');
        $data = $this->obj->where(['sl_id' => $id])->find();
        //类型
        $leeter_data = M('store_letter_type')->select();
        $type_data = [];
        $parameter_data = [];
        foreach($leeter_data as $k1 => $v1){
            $type_data[$v1['slt_id']] = $v1['type_name'];
            $parameter_data[$v1['slt_id']] = explode('#',$v1['parameter']);
        }
        //dump($parameter_data);
        $this->_arr['data'] = $data;
        $this->_arr['type_data'] = $type_data;
        $this->_arr['parameter_data'] = json_encode($parameter_data);
        $this->_showDisplay('Letter/newLetter');
    }

    /**
     * 删除
     */
    public function deleteLetter(){
        $id = I('id') ? I('id') : exit(json_encode(['status' => false,'msg' => '参数为空']));
        if (!is_numeric($id)) {
            exit(json_encode(['status' => false,'msg' => '无效参数']));
        }
        $status = $this->obj->where(['sl_id' => $id])->delete();
        exit(json_encode($status === false ? ['status' => false,'msg' => '删除失败'] : ['status' => true,'msg' => '删除成功']));
    }

    /**
     * 启用（禁用）
     */
    public function freezeTemplate(){
        $sl_id = I('sl_id') or exit(json_encode(['stat' => false,'msg' => '参数错误']));
        $slt_id = I('slt_id') or exit(json_encode(['stat' => false,'msg' => '参数错误']));
        $status = I('status');
        $sat = M('store_letter')->where(['sl_id' => $sl_id,'local_id'=>$this->local_id])->save(['status'=>$status]);
        if($sat === false){
            $this->ajaxReturn(['stat' => false,'msg' => '失败']);
        }
        if($status == 1){
            $f_status = 2;
            $sat = M('store_letter')->where(['slt_id' => $slt_id,'local_id'=>$this->local_id])->save(['status'=>$f_status]);
            //dump($sat);die;
            exit(json_encode($sat === false ? ['stat' => false,'msg' => '失败'] : ['stat' => true,'msg' => '成功']));
        }
        $this->ajaxReturn(['stat' => true,'msg' => '成功']);

    }

    /**
     * 发送短信
     * @param $count 内容
     * @return bool
     */


    public function __destruct()
    {
        unset($this->obj);
    }


}